package com.content.view.Frame.CBar
{
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.CButton.CButtonLevel2;
	import com.content.view.Component.CButton.Level1VO;
	import com.content.view.Component.CButton.Level2VO;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	
	public class CBarLevel2 extends UIObject implements IDisposable
	{
		public static const NAME:String							= 'CBarLevel2';
		public static const CHOOSE_CBUTTON:String				= NAME + "-" + 'ChooseCButton';
		
		private var _bgLevel1:Shape;
		private var _bgBmpData:BitmapData;
		private var _barWidth:int;
		private var _marginLeft:int;
		private var _btnMarginH:int;
		private var _btnMarginV:int;
		private var _btnHeight:int;
		
		private var _btnList:Array;
		private var _pSkin:SkinProxy;
		
		public function CBarLevel2(barWidth:int, marginLeft:int, btnMarginH:int, btnMarginV:int, btnHeight:int)
		{
			_marginLeft = marginLeft;
			_barWidth = barWidth;
			_btnMarginH = btnMarginH;
			_btnMarginV = btnMarginV;
			_btnHeight = btnHeight;
			
			_btnList = new Array();
			
			AddBG();
			this.addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		private function AddBG():void
		{
			this.alpha = 0;
			this.visible = false;
			
			_bgLevel1 = new Shape();
			addChild(_bgLevel1);
			
			var bg:Sprite = new SkinProxy.IMG_CBar_BG_CBarL2() as Sprite;
			_bgBmpData = new BitmapData(bg.width, bg.height, false);
			_bgBmpData.draw(bg);
		}
		
		private function UpdateButtons(l1VO:Level1VO):void{
			ClearButtons();
			
			var insertY:int;
			if(l1VO.Category =="Normal"){
				insertY = l1VO.PosY;
			}else{
				insertY = l1VO.PosY - (l1VO.SubVOList.length-1) * (_btnHeight + _btnMarginV);
			}
			for each(var l2VO:Level2VO in l1VO.SubVOList){
				var obj:Sprite;
				if(l2VO.ICOName != ""){
					var objClass:Class = _pSkin.GetClass(l2VO.ICOName);
					obj = new objClass() as Sprite;
				}
				var l2Btn:CButtonLevel2 = new CButtonLevel2( obj, l2VO, _barWidth - _btnMarginH*2, _btnHeight);
				addChild(l2Btn);
				l2Btn.x = _btnMarginH;
				l2Btn.y = insertY;
				insertY += int(_btnMarginV + l2Btn.height);
				
				_btnList.push(l2Btn);
			}
			
		}
		
		private function ClearButtons():void{
			if(_btnList.length <=0 ) return;
			var btn:CButtonLevel2;
			while (_btnList.length > 0) {
				btn = _btnList.pop() as CButtonLevel2;
				removeChild(btn);
				btn.Dispose();
			}
		}
		
		/* Event Handler
		* ------------------------------------------------------------- */
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is CButtonLevel2){
				var togItem:CButtonLevel2 = evt.target as CButtonLevel2;				
				dispatchEvent(new DataEvent(CHOOSE_CBUTTON, true, false, togItem.MPID));
			}
		}
		
		/* Public Method
		* ------------------------------------------------------------- */
		public function Init(pSkin:SkinProxy):void{
			_pSkin = pSkin;
		}
		
		public function SetLayout(sHeight:int):void{
			var verticalSpan:int = 10;
			// Redraw the BG
			_bgLevel1.graphics.clear();
			_bgLevel1.graphics.beginBitmapFill(_bgBmpData, null, true);
			_bgLevel1.graphics.drawRect(0,0,_barWidth, sHeight);
		}
		
		public function Show(l1VO:Level1VO, delay:Number = 0, duration:Number = 0.5):void{
			UpdateButtons(l1VO);
			var posX:int = _marginLeft - _barWidth;
			this.x = posX;
			TweenLite.to( this, duration, { x: _marginLeft, autoAlpha:1, delay: delay } );
		}		
		public function Hide():void	{
			if(this.alpha < 1.0) return;	// 保证只有Bar完全出来时，才响应Hide指令，否则用户只要有点击，就会Hide
			var posX:int = _marginLeft - _barWidth;
			TweenLite.to( this, 0.5, { x: posX, autoAlpha:0, delay: 0 } );
		}
		
		public function Dispose():void{
			_bgBmpData.dispose();
			_bgBmpData = null;
		}
	}
}